<?php

namespace Baike\Controller;

use Book\Model\BookModel;
use Common\Controller\ApiController;
use Common\Model\ModelException;
use Util\ModelUtils;
use Util\Tree;

class BaikeController extends ApiController {

    protected $Model = NULL;

    public function _initialize() {
        parent::_initialize();
        $this->Model = D(CONTROLLER_NAME);
    }

    /*
     * 知识首页
     */

    public function baikeTag() {
        $period = D('Period')->select();
        ModelUtils::hasMany($period, ['foreignKey' => 'period_id', 'relation' => 'PeriodTag', 'refKey' => 'period_id', 'name' => 'toptag']);
        $input = $this->request();
        $sex = $input['sex'] == 1 ? 2 : 1;
        $map['sex'] = array('neq', $sex);
        $baikeTag = D('BaikeTag')->where($map)->order('orderby asc')->select();
        $tree = new Tree('tag_id', 'p_tag_id');
        $tree->load($baikeTag);
        $tags = $tree->deepTree();
        foreach ($period as $key => &$value) {
            foreach ($value['toptag'] as $kk => $vv) {
                foreach ($tags as $k => $v) {
                    if ($v['tag_id'] == $vv['tag_id']) {
                        $period[$key]['taglist'][] = $v;
                    }
                }
            }
        }
        $period = transform('Baike', 'listview', $period);
        $this->response($period);
    }

    /*
     * 百科内容列表
     */

    public function baike() {
        $input = $this->request();
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 30);
        $perPage > 30 && $perPage = 30;
        $orderby = filter($input['orderby'], 'add_time');
        $direction = filter($input['direction'], 'desc');
        $where['tag_id'] = $input['tag_id'];
        $count = $this->Model->where($where)->count();
        if ($count > 0) {
            $baike = $this->Model
                    ->field('id,title,cover')
                    ->where($where)
                    ->page($page, $perPage)
                    ->order("{$orderby} {$direction}")
                    ->select();
        }

        $pagination = pagination($count, $perPage, $page);
        $this->response($baike, $pagination);
    }

    /*
     * 知识库内容详情
     */

    public function baikeDetail() {
        $input = $this->request();
        $id = $input['id'];
        $baike = $this->Model->find();
//        $baike['content'] = ($baike['content']);
        $baike['likeid'] = '';
        if (!empty($this->user['id'])) {
            $where['baike_id'] = $id;
            $where['user_id'] = $this->user['id'];
            $like = D('BaikeLike')->where($where)->find();
            if ($like) {
                $baike['likeid'] = $like['id'];
            }
        }
        $this->response($baike);
    }

    /*
     * 百科收藏
     */

    public function like() {
        $input = $this->request();
        $data['user_id'] = $this->user['id'];
        $data['baike_id'] = $input['id'];
        $BaikeLike = D('BaikeLike');
        $result = $BaikeLike->where($data)->find();
        if ($result) {
            $id = $result['id'];
        } else {
            $data['add_time'] = time();
            $BaikeLike->create($data);
            $id = $BaikeLike->add();
        }
        $re['message'] = '收藏成功';
        $re['id'] = $id;
        $this->createdResponse($re); //无论失败成功都正确返回
    }

    /*
     * 取消收藏
     */

    public function delLike() {
        $input = $this->request();
        $id = $input['id'];
        $BaikeLike = D('BaikeLike');
        $BaikeLike->delete($id);
        $this->createdResponse();
    }

    /*
     * 收藏列表
     */

    public function likelist() {
        $input = $this->request();
        $keyword = filter($input['keyword'], '');
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 10);
        $perPage > 30 && $perPage = 30;
        $orderby = filter($input['orderby'], 'add_time');
        $direction = filter($input['direction'], 'desc');
        $where['user_id'] = $this->user['id'];
        $baikeLikeModel = D('BaikeLike');
        $count = $baikeLikeModel->where($where)->count();
        if ($count > 0) {
            $baikeLike = $baikeLikeModel
                    ->where($where)
                    ->page($page, $perPage)
                    ->order("{$orderby} {$direction}")
                    ->select();
            ModelUtils::hasMany($baikeLike, ['foreignKey' => 'baike_id', 'relation' => 'Baike', 'refKey' => 'id']);
        }

        $pagination = pagination($count, $perPage, $page);
        $this->response($baikeLike, $pagination);
    }

}
